{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 部署HuggingFace模型\n",
    "\n",
    "HuggingFace是一个开源的模型社区，机器学习开发者在社区中可以分享、发现和使用各类机器学习模型。\n",
    "\n",
    "本文将介绍如何将HuggingFace社区的模型部署到PAI创建模型推理服务。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 安装和配置SDK\n",
    "\n",
    "\n",
    "我们需要首先安装PAI Python SDK以运行本示例。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "!python -m pip install --upgrade alipai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "\n",
    "SDK需要配置访问阿里云服务需要的AccessKey，以及当前使用的工作空间和OSS Bucket。在PAI SDK安装之后，通过在**命令行终端** 中执行以下命令，按照引导配置密钥、工作空间等信息。\n",
    "\n",
    "\n",
    "```shell\n",
    "\n",
    "# 以下命令，请在 命令行终端 中执行.\n",
    "\n",
    "python -m pai.toolkit.config\n",
    "\n",
    "```\n",
    "\n",
    "我们可以通过以下代码验证配置是否已生效。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pai\n",
    "from pai.session import get_default_session\n",
    "\n",
    "print(pai.__version__)\n",
    "\n",
    "sess = get_default_session()\n",
    "\n",
    "# 获取配置的工作空间信息\n",
    "assert sess.workspace_name is not None\n",
    "print(sess.workspace_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 部署HuggingFace模型\n",
    "\n",
    "在本示例中，我们将使用HuggingFace社区提供的情感分类模型 [distilbert-base-uncased-finetuned-sst-2-english](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english)部署一个模型在线服务，他支持将一段英文文本分类为正面或负面情感。\n",
    "\n",
    "通过相应的[模型的详情页](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/tree/main)，我们可以获取部署模型所需的信息，包括模型ID(``MODEL_ID``)、模型任务类型(``TASK``)、模型版本(``REVISION``)。\n",
    "\n",
    "![](../../images/huggingface-model.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过PAI Python SDK提供的``HuggingFaceModel``，我们可以轻松地将HuggingFace社区的模型部署到PAI上。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pai.huggingface import HuggingFaceModel\n",
    "\n",
    "\n",
    "# 初始化一个HuggingFaceModel\n",
    "m = HuggingFaceModel(\n",
    "    command=\"python app.py\",  # 模型服务启动命令\n",
    "    transformers_version=\"latest\",  # 使用的transformers版本, 'latest'表示使用PAI目前支持的最新的版本\n",
    "    environment_variables={\n",
    "        \"MODEL_ID\": \"distilbert-base-uncased-finetuned-sst-2-english\",  # 部署模型的ID\n",
    "        \"TASK\": \"text-classification\",  # 部署的模型任务类型\n",
    "        \"REVISION\": \"main\",  # 部署模型的版本信息\n",
    "    },\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pai.common.utils import random_str\n",
    "\n",
    "\n",
    "# 部署模型，创建一个模型在线服务\n",
    "p = m.deploy(\n",
    "    service_name=f\"hf_model_deploy_{random_str(n=8)}\",  # 模型服务的名称（地域内唯一）\n",
    "    instance_type=\"ecs.g6.large\",  # 模型服务使用的机器实例规格\n",
    "    options={\n",
    "        \"enable_webservice\": True,  # 以AIWeb应用的模式启动，支持用户在Web浏览器上使用模型在线服务\n",
    "    },\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p.predict(data={\"data\": [\"I love you\"]})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试完成之后，删除服务，释放机器资源。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p.delete_service()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
